//package com.quanshi.jingdian.utils;
//
//import com.quanshi.common.core.domain.AjaxResult;
//import com.quanshi.jingdian.domain.JdElectricityRecords;
//import com.quanshi.jingdian.domain.JdRelaying;
//import com.quanshi.jingdian.domain.JdStation;
//import com.quanshi.jingdian.domain.vo.UpdateElectricityUserVo;
//import com.quanshi.jingdian.domain.vo.UpdateElectricityVo;
//import com.quanshi.jingdian.service.*;
//import lombok.extern.log4j.Log4j2;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.net.ServerSocket;
//import java.util.*;
//
///**
// * 通信长连接  端口管理器
// *
// * @author quanshi
// * @date 2023-02-22
// */
//
//
//@Log4j2
//public class PortListenerManager {
//
//    @Resource
//    public IJdElectricityService electricityService;
//    @Resource
//    public IJdElectricityRecordsService recordsService;
//    @Resource
//    public IJdStationService stationService;
//    @Resource
//    public IJdRelayingService relayingService;
//    @Resource
//    public IJdProductService productService;
//    @Autowired
//    public IJdOperationLogService operationLogService;
//    @Autowired
//    public IJdElectricityStatusService electricityStatusService;
//    @Resource
//    public RedisUtils redisUtils;
//
//    @PostConstruct
//    public void init() {
//        PortListenerManager manager = this;
//        manager.electricityService = this.electricityService;
//        manager.recordsService = this.recordsService;
//        manager.stationService = this.stationService;
//        manager.relayingService = this.relayingService;
//        manager.productService = this.productService;
//        manager.operationLogService = this.operationLogService;
//        manager.electricityStatusService = this.electricityStatusService;
//        manager.redisUtils = this.redisUtils;
//    }
//
//    //正在监听的端口
//    private final List<PortListener> listeners = new ArrayList<>();
//
//    //避免出现重复的端口
//    private final Set<Integer> currentPorts = new HashSet<>();
//
//    //调用此接口 每个正在使用的接口 从数据库中获取数据进行更新
//    public void notifyListeners() {
//        for (PortListener listener : listeners) {
//            listener.update();
//        }
//    }
//
//    /**
//     * 初始启动  通过获取所有端口从而完成所有的端口的监控
//     */
//    public PortListenerManager(List<JdRelaying> jdRelayings){
//        // 启动初始的监听线程
//        for (JdRelaying relaying : jdRelayings) {
//            try {
//                PortListener listener = new PortListener(relaying.getRelayingPort(), this);
//                Thread thread = new Thread(listener);
//                thread.start();
//                listeners.add(listener);
//                currentPorts.add(relaying.getRelayingPort());
//            } catch (Exception e) {
//                // 处理启动监听器失败的情况
////                e.printStackTrace();
//                log.info("启动监听器失败,端口为:{}",relaying.getRelayingPort());
//            }
//        }
//    }
//
//    /**
//     * 启动端口
//     */
//    public void startPort(JdRelaying jdRelaying) {
//        if (!currentPorts.contains(jdRelaying.getRelayingPort())) {
//            PortListener listener = new PortListener(jdRelaying.getRelayingPort(),this);
//            Thread thread = new Thread(listener);
//            thread.start();
//            listeners.add(listener);
//            currentPorts.add(jdRelaying.getRelayingPort());
//        }
//    }
//
//    /**
//     * 停止端口
//     */
//    public void stopPort(JdRelaying jdRelaying) {
//        Iterator<PortListener> iterator = listeners.iterator();
//        while (iterator.hasNext()) {
//            PortListener listener = iterator.next();
//            if (jdRelaying.getRelayingPort() == listener.getPort()) {
//                listener.stopPort();
//                iterator.remove(); // 使用迭代器安全地删除元素
//                currentPorts.remove(jdRelaying.getRelayingPort());
//            }
//        }
//    }
//    /**
//     * 发送修改设备信息
//     */
//    public void sendData(UpdateElectricityUserVo electricityUserVo) {
//        for (PortListener listener : listeners) {
//            if (electricityUserVo.getRelayingPort() == listener.getPort()) {
//                listener.sendData(electricityUserVo);
//            }
//        }
//    }
//    /**
//     * 获取监控中的端口
//     */
//    public List<PortListener> getListenerList() {
//        return listeners;
//    }
//
//}
